//
//  SAMemberMessageVC.m
//  saasRetail
//
//  Created by mac on 2023/5/28.
//

#import "SAMemberMessageVC.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SASmsBalanceAPI.h"
#import "SABuyMessageVC.h"
#import "SAMemberMessageCell.h"
#import "SAMemberSendGroupVC.h"
#import "SASendRecordVC.h"
#import "SASmsAutoSendListAPI.h"
#import "SASmsAutoSendListModel.h"
#import "SASystemSettingsListAPI.h"
#import "SASystemSettingsCheckAPI.h"
#import "SACommoditySpecialFeesModel.h"
#import "HDDatePickerView.h"

@interface SAMemberMessageVC ()<UITableViewDelegate, UITableViewDataSource, HTTPAPIDelegate>

@property (nonatomic, strong) UIView *topBgView;
@property (nonatomic, strong) UILabel *availableLab;
@property (nonatomic, strong) UIImageView *availableImageV;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) UIButton *purchaseBtn;

@property (nonatomic, strong) UIButton *groupBtn;//发群
@property (nonatomic, strong) UIButton *sendBtn;//发送记录
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UIView *buleLine;
@property (nonatomic, strong) UILabel *tipLab;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SASmsBalanceAPI *smsBalanceAPI;
@property (nonatomic, strong) SASmsAutoSendListAPI *smsAutoSendListAPI;
@property (nonatomic, strong) NSMutableArray<SASmsAutoSendListModel *> *dataMarr;
@property (nonatomic, strong) SASystemSettingsListAPI *systemSettingsListAPI;
@property (nonatomic, strong) NSString *settingsId;//上传自动发送id
@property (nonatomic, strong) SASystemSettingsCheckAPI *systemSettingsCheckAPI;
@property (nonatomic, strong) SACommoditySpecialFeesModel *dateModel;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@end

@implementation SAMemberMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.smsBalanceAPI == api) {
        self.numLab.text = [NSString stringWithFormat:@"%@条",data[@"data"][@"smsBalance"]];
        [self.numLab setAttributedStringWithSubString:@"条" font:kFont(14)];
    }else if (self.smsAutoSendListAPI == api) {
        self.dataMarr = [SASmsAutoSendListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        [self.tableView reloadData];
    }else if (self.systemSettingsListAPI == api) {
        self.settingsId = [NSString stringWithFormat:@"%@",data[@"data"][@"member_msg_notify"][@"settingsId"]];
        NSMutableArray<SACommoditySpecialFeesModel *> *marr = [SACommoditySpecialFeesModel mj_objectArrayWithKeyValuesArray:data[@"data"][@"member_points_setting"][@"childSettings"]];
        for (SACommoditySpecialFeesModel *model in marr) {
            if ([model.code isEqualToString:@"clearing_notify_date"]) {
                self.dateModel = model;
            }
        }
    }else if (self.systemSettingsCheckAPI == api) {
        
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf
    SAMemberMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAMemberMessageCell" forIndexPath:indexPath];
    cell.date = self.dateModel.value;
    cell.model = self.dataMarr[indexPath.section];
    cell.selectBlock = ^{
        [weakSelf systemSettings];
    };
    cell.dateBlock = ^{
        [weakSelf dateSettings];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(150);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

#pragma mark - event
-(void)clickedPurchaseBtn:(UIButton *)sender {
    
    SABuyMessageVC *vc = [SABuyMessageVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickedGroupBtn:(UIButton *)sender {
    
    SAMemberSendGroupVC *vc = [SAMemberSendGroupVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickedSendBtn:(UIButton *)sender {
    
    SASendRecordVC *vc = [SASendRecordVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

//修改自定义发送开关
-(void)systemSettings {
    NSMutableArray *marr = [NSMutableArray array];
    for (SASmsAutoSendListModel *model in self.dataMarr) {
        [marr addObject:model.enabled];
    }
    self.systemSettingsCheckAPI.value = [marr componentsJoinedByString:@","];
    self.systemSettingsCheckAPI.parentId = @"";
    self.systemSettingsCheckAPI.settingsId = self.settingsId;
    [self.systemSettingsCheckAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(void)dateSettings {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.dateModel.value];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
        weakSelf.dateModel.value = selectedDate;
        [weakSelf.tableView reloadData];
        weakSelf.systemSettingsCheckAPI.value = selectedDate;
        weakSelf.systemSettingsCheckAPI.parentId = weakSelf.dateModel.parentId;
        weakSelf.systemSettingsCheckAPI.settingsId = weakSelf.dateModel.settingsId;
        [weakSelf.systemSettingsCheckAPI sendRequestToServerWithActivityViewAndMask:POST];
    };
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"会员短信"];
    [self setBgImage:nil];
    self.view.backgroundColor = kBgColor;
    
    [self.view addSubview:self.topBgView];
    
    [self.topBgView addSubview:self.availableLab];
    [self.topBgView addSubview:self.availableImageV];
    [self.topBgView addSubview:self.numLab];
    [self.topBgView addSubview:self.purchaseBtn];
    
    [self.view addSubview:self.groupBtn];
    [self.view addSubview:self.sendBtn];
    [self.view addSubview:self.line];
    [self.view addSubview:self.buleLine];
    [self.view addSubview:self.tipLab];
    [self.view addSubview:self.tableView];
    
    [self.numLab setAttributedStringWithSubString:@"条" font:kFont(14)];
    
    CGFloat imageWidth = self.purchaseBtn.imageView.bounds.size.width;
    CGFloat labelWidth = self.purchaseBtn.titleLabel.bounds.size.width;
    self.purchaseBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
    self.purchaseBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
//    self.purchaseBtn.hidden = YES;//业务需求暂时屏蔽
    
    [self.smsBalanceAPI sendRequestToServerWithType:GET];
    [self.smsAutoSendListAPI sendRequestToServerWithType:GET];
    [self.systemSettingsListAPI sendRequestToServerWithType:GET];
    
}

-(UIView *)topBgView {
    if (!_topBgView) {
        _topBgView = [[UIView alloc] initWithFrame:SARectMake(10, StatusHeight + 10, kBaseScreenWidth - 20, 100)];
        _topBgView.backgroundColor = kBuleColor;
    }
    return _topBgView;
}

-(UILabel *)availableLab {
    if (!_availableLab) {
        _availableLab = [[UILabel alloc] initWithFrame:SARectMake(20, 20, 100, 20)];
        _availableLab.text = @"可用短信";
        _availableLab.font = kFont(15);
        _availableLab.textColor = [UIColor whiteColor];
    }
    return _availableLab;
}

- (UIImageView *)availableImageV {
    if (!_availableImageV) {
        _availableImageV = [[UIImageView alloc] initWithFrame:SARectMake(90, 20, 20, 20)];
        _availableImageV.image = [UIImage imageNamed:@"SAMemberMail"];
    }
    return _availableImageV;
}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] initWithFrame:SARectMake(20, 55, 200, 26)];
        _numLab.text = @"0条";
        _numLab.font = kBFont(25);
        _numLab.textColor = [UIColor whiteColor];
    }
    return _numLab;
}

-(UIButton *)purchaseBtn {
    if (!_purchaseBtn) {
        _purchaseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _purchaseBtn.backgroundColor = [UIColor whiteColor];
        [_purchaseBtn setTitle:@"购买套餐" forState:UIControlStateNormal];
        [_purchaseBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
        [_purchaseBtn setImage:[UIImage imageNamed:@"sale_car"] forState:UIControlStateNormal];
        _purchaseBtn.titleLabel.font = kFont(16);
        _purchaseBtn.frame = SARectMake(kBaseScreenWidth - 160, 52, 120, 30);
        _purchaseBtn.layer.cornerRadius = kScaleNum(15);
        _purchaseBtn.layer.masksToBounds = YES;
        [_purchaseBtn addTarget:self action:@selector(clickedPurchaseBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _purchaseBtn;
}

-(UIButton *)groupBtn {
    if (!_groupBtn) {
        _groupBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _groupBtn.frame = SARectMake(0, StatusHeight + 130, 187, 60);
        [_groupBtn setImage:[UIImage imageNamed:@"sendMail"] forState:UIControlStateNormal];
        [_groupBtn setTitle:@" 自定义群发" forState:UIControlStateNormal];
        [_groupBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        _groupBtn.titleLabel.font = kFont(18);
        [_groupBtn addTarget:self action:@selector(clickedGroupBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _groupBtn;
}

-(UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendBtn.frame = SARectMake(188, StatusHeight + 130, 187, 60);
        [_sendBtn setImage:[UIImage imageNamed:@"sendRecord"] forState:UIControlStateNormal];
        [_sendBtn setTitle:@" 发送记录" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = kFont(18);
        [_sendBtn addTarget:self action:@selector(clickedSendBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(187, StatusHeight + 135, 1, 50)];
        _line.backgroundColor = kColor(200, 200, 200, 1);
    }
    return _line;
}

-(UIView *)buleLine {
    if (!_buleLine) {
        _buleLine = [[UIView alloc] initWithFrame:SARectMake(15, StatusHeight + 210, 3, 15)];
        _buleLine.backgroundColor = kBuleColor;
    }
    return _buleLine;
}

-(UILabel *)tipLab {
    if (!_tipLab) {
        _tipLab = [[UILabel alloc] initWithFrame:SARectMake(25, StatusHeight + 210, 300, 15)];
        _tipLab.font = kFont(15);
        _tipLab.textColor = kBlackColor;
        _tipLab.text = @"自动发送";
    }
    return _tipLab;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 235), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 235)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAMemberMessageCell class] forCellReuseIdentifier:@"SAMemberMessageCell"];
    }
    return _tableView;
}

-(SASmsBalanceAPI *)smsBalanceAPI {
    if (!_smsBalanceAPI) {
        _smsBalanceAPI = [SASmsBalanceAPI apiWithDelegate:self];
    }
    return _smsBalanceAPI;
}

-(SASmsAutoSendListAPI *)smsAutoSendListAPI {
    if (!_smsAutoSendListAPI) {
        _smsAutoSendListAPI = [SASmsAutoSendListAPI apiWithDelegate:self];
    }
    return _smsAutoSendListAPI;
}

-(NSMutableArray<SASmsAutoSendListModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(SASystemSettingsListAPI *)systemSettingsListAPI {
    if (!_systemSettingsListAPI) {
        _systemSettingsListAPI = [SASystemSettingsListAPI apiWithDelegate:self];
    }
    return _systemSettingsListAPI;
}

-(SASystemSettingsCheckAPI *)systemSettingsCheckAPI {
    if (!_systemSettingsCheckAPI) {
        _systemSettingsCheckAPI = [SASystemSettingsCheckAPI apiWithDelegate:self];
    }
    return _systemSettingsCheckAPI;
}

-(NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [NSDateFormatter new];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

@end
